package cn.wsalix.templet.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.service.impl.SimpleServiceImpl;
import cn.wsalix.templet.entity.SiteInfo;
import cn.wsalix.templet.entity.SitePage;
import cn.wsalix.templet.form.SitePageForm;
import cn.wsalix.templet.repository.SitePageRepository;
import cn.wsalix.templet.service.SitePageService;
import cn.anlaser.utils.DynamicSpecifications;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;

@Service("sitePageService")
@Transactional(readOnly = true)
public class SitePageServiceImpl extends
		SimpleServiceImpl<SitePageForm, SitePageRepository, SitePage> implements
		SitePageService {

	@Autowired
	private SitePageRepository sitePageRepository;

	@Override
	public SitePageRepository getRepository() {
		return sitePageRepository;
	}

	@Override
	public String module() {
		return "templet/site_page";
	}

	@Override
	public List<SitePage> findBySite(SiteInfo siteInfo) {
		return sitePageRepository.findBySiteId(siteInfo.getId());
	}

	@Override
	public Page<SitePage> findByStId(Long siteId, Pageable pageable) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		filters.put("siteInfo.id", new SearchFilter("siteInfo.id", Operator.EQ,
				siteId));
		Specification<SitePage> spec = DynamicSpecifications.bySearchFilter(
				filters.values(), SitePage.class);

		return sitePageRepository.findAll(spec, pageable);
	}

	/*
	 * @Transactional
	 * 
	 * @Override public SitePage findByMenuAndSt(SysMenuStructure menuStructure,
	 * SiteTemplet siteTemplet) { SitePage
	 * sitePage=sitePageRepository.findByMenuIdAndStIdOne
	 * (menuStructure.getId(),siteTemplet.getId()); if(sitePage==null){
	 * sitePage=
	 * sitePageRepository.findBySiIdAndUrlPatternOne(siteTemplet.getId()
	 * ,menuStructure.getReqUrl()); } if(sitePage==null){//sitepage 不存在,创建
	 * sitePage=new SitePage(); sitePage.setSiteTemplet(siteTemplet);
	 * sitePage.setMenuStructure(menuStructure);
	 * sitePage.setReqUrlPattern(menuStructure.getReqUrl());
	 * sitePage.setTempFile(siteTemplet.getDefaultPage());
	 * sitePage=sitePageRepository.save(sitePage); } return sitePage; }
	 */
	@Transactional
	@Override
	public SitePage saveTempFileById(Long sitePageId, String tempFile) {
		SitePage sitePage = findById(sitePageId);
		sitePage.setTempFile(tempFile);
		sitePage = edit(sitePage);
		return sitePage;
	}

	@Override
	public SitePage findByMenuUrl(String menuUrl) {
		return sitePageRepository.findByMenuUrl(menuUrl);
	}

	@Override
	public Class<SitePage> getEntityClazz() {
		return SitePage.class;
	}

	@Override
	public List<SitePage> findBySiteId(Long siteId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(SitePageForm form) {
		// TODO Auto-generated method stub
		return null;
	}

}
